<?php
#Config
$db_host = 'localhost';
$db_name = 'fengine';
$db_username = 'ranger';
$db_password = 'qazwsx';
$db_prefix = '';



$login=$_GET['login'];
$password=$_GET['password'];

$login=str_replace("'","",$login);
if(! preg_match('/\w+$/',$login) ) exit;
$password=str_replace("'","",$password);
if(! preg_match('/\w*$/',$password) ) exit;

#FEngine
#Connect to DB
mysql_connect($db_host, $db_username, $db_password);
mysql_select_db($db_name) or die(mysql_error());



list ($id,$group)=login($login,$password);

$request=$_GET['req'];
if($request=="reg") {
	$Rlogin=$_GET['login'];
	if(! preg_match('/\w+$/',$Rlogin) ) die("Wrong login!");
	$Rname=$_GET['name'];
	if(! preg_match('/[\w\s]+$/',$Rname) ) die("Wrong name!");
	$Rpassword=$_GET['password'];
	if(! preg_match('/\w+$/',$Rpassword) ) die("Wrong password!");
	$res=mysql_query("INSERT INTO  `users` (`login`,`password`,`group`,`username`) VALUES ('".$Rlogin."', '".md5(sha1(sha1(md5($Rpassword))))."','1', '".$Rname."')") or die(mysql_error());
	print $Rlogin."#".$Rname."#".$Rpassword."\n";
	
	mysql_close();
	exit;
}

if(!$id) {
	print "login error";
	exit;
} 

if($request=="cats") {
	$res=mysql_query("SELECT * FROM `".$db_prefix."cats`") or die(mysql_error());
	while($row=mysql_fetch_array($res)) {
		print $row['id']."#".$row['name']."\n";
	}
} else if($request=="themes") {
	$catid=intval($_GET['catid']);
	$res=mysql_query("SELECT * FROM `".$db_prefix."themes` WHERE catid = ".$catid." ORDER BY id DESC") or die(mysql_error());
	while($row=mysql_fetch_array($res)) {
		print $row['id']."#".$row['name']."#".$row['date']."#".$row['userid']."\n";
	}
} else if($request=="messages") {
	$themeid=intval($_GET['themeid']);
	$res=mysql_query("SELECT * FROM `".$db_prefix."messages` WHERE `themeid` = ".$themeid." ORDER BY id") or die(mysql_error());
	while($row=mysql_fetch_array($res)) {
		print $row['id']."#".$row['userid']."#".$row['date']."#".$row['text']."\n";
	}
} else if($request=="msg2theme") {
	$themeid=intval($_GET['themeid']);
	$text=$_GET['text'];
	#FILTER
	$text=str_replace("'","&quot;",$text);
	
	$res=mysql_query("INSERT INTO  `messages` (`text`,`userid`,`themeid`) VALUES ('".$text."',  '".$id."','".$themeid."')") or die(mysql_error());	
} else if($request=="theme2cat") {
	$catid=intval($_GET['catid']);
	$themename=$_GET['themename'];
	$themename=str_replace("'","&quot;",$themename);
	$res=mysql_query("INSERT INTO  `themes` (`name`,`userid`,`catid`) VALUES ('".$themename."',  '".$id."','".$catid."')") or die(mysql_error());	
} else if($request=="users") {
	$uids=$_GET['uids'];
	$uid=split(",",$uids);
	$request="SELECT * FROM `".$db_prefix."users` WHERE ";
	foreach ($uid as $value) {
		$request.="id=".intval($value);
		if($value!=end($uid)) $request.=" OR ";
	}
	$res=mysql_query($request) or die(mysql_error());
	while($row=mysql_fetch_array($res)) {
		print $row["id"]."#".$row["username"]."#".$row["ava"]."\n"; 
	}
} else if($request=="rmtheme") {
	$themeid=intval($_GET['themeid']);
	$res=mysql_query("DELETE FROM `".$db_prefix."themes` WHERE (`id` = ".$themeid." AND `userid` = ".$id.")") or die(mysql_error());
	print "done\n";
} else if($request=="rmmess") {
	$messid=intval($_GET['messid']);
	if($group=="2") {
		$res=mysql_query("DELETE FROM `".$db_prefix."messages` WHERE (`id` = ".$messid.")") or die(mysql_error());	
	} else {
		$res=mysql_query("DELETE FROM `".$db_prefix."messages` WHERE (`id` = ".$messid." AND `userid` = ".$id.")") or die(mysql_error());
	}
	print "done\n";
} else if($request=="editmess") {
	$messid=intval($_GET['messid']);
	$text=$_GET['text'];
	#FILTER
	$text=str_replace("'","&quot;",$text);
	if($group=="2") {
		$res=mysql_query("UPDATE `".$db_prefix."messages` SET `text` = '".$text."' WHERE (`id` = ".$messid.")") or die(mysql_error());
	} else {
		$res=mysql_query("UPDATE `".$db_prefix."messages` SET `text` = '".$text."' WHERE (`id` = ".$messid." AND `userid` = ".$id." )") or die(mysql_error());
	}
	print "done\n";
} else if($request=="catname") {
	$catid=intval($_GET['catid']);
	$res=mysql_query("SELECT name FROM `".$db_prefix."cats` WHERE `id` = ".$catid) or die(mysql_error());
	$row=mysql_fetch_array($res);
	print $row['name'];
} else if($request=="themename") {
	$themeid=intval($_GET['themeid']);
	$res=mysql_query("SELECT name FROM `".$db_prefix."themes` WHERE `id` = ".$themeid) or die(mysql_error());
	$row=mysql_fetch_array($res);
	print $row['name'];
} else if($request=="profile") {
	$uid=intval($_GET['uid']);
	if($uid=="0") {
		$uid=$id;
	}
	$res=mysql_query("SELECT * FROM `".$db_prefix."users` WHERE (`id` = '".$uid."' )") or die(mysql_error());
	$row=mysql_fetch_array($res);
	print $row["id"]."#".$row["username"]."#".$row["login"]."#".$row["group"]."#".$row["ava"]."#".$row["description"];
} else if($request=="saveprofile") {
	$name=$_GET['name'];
	if(! preg_match('/[\w\s]+$/',$name) ) die("Wrong name!");
	$ava=$_GET['ava'];
	$ava=str_replace("'","&quot;",$ava);
	$description=$_GET['desc'];
	$description=str_replace("'","&quot;",$description);
	$res=mysql_query("UPDATE `".$db_prefix."users` SET `username` = '".$name."', `ava` = '".$ava."', `description` = '".$description."'  WHERE (`id` = ".$id.")") or die(mysql_error());
}

mysql_close();

function login($login,$password) {
	$res=mysql_query("SELECT * FROM `".$db_prefix."users` WHERE (`login` = '".$login."' AND `password` = '".md5(sha1(sha1(md5($password))))."')") or die(mysql_error());
	if(mysql_num_rows($res)==0) {
		return false;
	} else {
		$row=mysql_fetch_array($res);
		return array($row['id'],$row['group']);
	}
}

?>
